package org.test.myTree.AbstractTree;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class AbstractTree {
    protected static Log log = LogFactory.getLog(AbstractTree.class);

    protected Map<String,TreeNode> treeNodeMaps = new HashMap<String,TreeNode>();
    protected TreeNode root;

    /**
     * root if it's parent is empty
     * @param nodes 
     */
    protected void reload(List nodes) {
        log.info("tree will start reload all data");
        
        synchronized (this) {
            // initialize
            treeNodeMaps.clear();
            root = null;

            List treeNodes = new Vector(nodes.size());
            for (int i = 0; i < nodes.size(); i++) {
                TreeNode node = this.transform(nodes.get(i)); // transform  //dgm,表示不懂transform意思 ！！。。这里是 把userdept对象转为treenote对象 
                treeNodes.add(node);
                node.setTree(this);		
                treeNodeMaps.put(node.getNodeId(), node);
            }	//dgm,这个for把传进来的list<UserDept>参数转化到了一个list<TreeNode> treeNodes和Map<String,TreeNode> treeNodeMaps
            
            for (int i = 0; i < treeNodes.size(); i++) {  
            	TreeNode node = (TreeNode) treeNodes.get(i);
                String parentId = node.getParentId();
                if (this.isRootNode(node)) {
                    if (root == null) {
                        root = node;
                    } else {
                        log.error("find more then one root node. ignore.");
                    }
                } else {
                    TreeNode parent = (TreeNode) treeNodeMaps.get(parentId);		//dgm,得到当前的parent
                    if (parent != null) {
                        parent.addChild(node);
                        node.setParent(parent);
                    } else {
                        log.warn("node [id=" + node.getNodeId() + "]: missing parent node.");
                    }
                }
            }
        }
        if (root == null) {
            log.error("the root node is not be defined");
        }
    }

    protected boolean isRootNode(TreeNode node) {
        return StringUtils.isBlank(node.getParentId());
    }

    public TreeNode getRootNode() {
        return root;
    }

    public TreeNode getTreeNode(String nodeId) {
    	if(nodeId==null) return null;
    	TreeNode node = (TreeNode) treeNodeMaps.get(nodeId);
  /*
    	for(String key:treeNodeMaps.keySet())
    		{
    		System.out.println("key: "+key+" nodeId: "+nodeId+" nodeId=key? "+(nodeId==key)+" nodeId equal key?"+(nodeId.equals(key)));
    			if(nodeId.equals(key));
    			{
    					node = (TreeNode)treeNodeMaps.get(key);
    			System.out.println("AbstractTree: "+node.getNodeId());
    			}
    		}
  */
    //	System.out.println("nodeId:  "+node.getNodeId()+"  最新错误："+node);
        return node;
    }

    public void addTreeNode(TreeNode node) {
        synchronized (this) {
        	try{
                treeNodeMaps.put(node.getNodeId(), node);

                String parentId = node.getParentId();
                if (StringUtils.isNotBlank(parentId)) {
                    TreeNode parent = getTreeNode(parentId);
                    if (parent != null) {
                    	System.out.println("抽象树：parent id: "+parent.getNodeId()+" 加入的结点id："+node.getNodeId());
                        parent.addChild(node);
                        node.setParent(parent);
                    } else {
                        log.error("parent cannot be found: " + node.getParentId());
                    }
                } else {
                    if (root == null) {
                        root = node;
                    } else {
                        log.error("find more then one root node. ignore.");
                    }
                }
        	}catch(Exception e){e.printStackTrace();}

        }
    }

    public void deleteTreeNode(String nodeId) {
        synchronized (this) {
            TreeNode node = getTreeNode(nodeId);
            if (node == null)
                throw new IllegalArgumentException(nodeId + " cannot be found.");

            if (node.getParent() == null) {
                root = null;
                treeNodeMaps.clear();
                log.warn("the root node has been removed.");
            } else {
                node.getParent().getChildren().remove(node);

                treeNodeMaps.remove(nodeId);
                List children = node.getAllChildren();
                for (int i = 0; i < children.size(); i++) {
                    TreeNode n = (TreeNode) children.get(i);
                    treeNodeMaps.remove(n.getNodeId());
                }
            }
        }
    }
    /**
     * <pre>
     * Usage: Office -&gt;
     * 
     * public TreeNode transform(Object info) {
     *     OfficeInfo office_info = (OfficeInfo) info;
     *     TreeNode node = new TreeNode();
     *     node.setNodeId(office_info.getOfficeId());
     *     node.setParentId(office_info.getParentId());
     *     node.setBindData(office_info);
     *     return node;
     * }
     * </pre>
     */
    protected abstract TreeNode transform(Object info);
    
//    public void addDeptTreeNode(Dept dept) {
//		addTreeNode(transform(dept));
//	}
//    public void addDeptTreeNode(UserDept userdept) {
//		addTreeNode(transform(userdept));
//	}
//	
//	public void deleteDeptTreeNode(Dept dept) {
//		deleteTreeNode(dept.getDId());
//	}
//	public void deleteUserDeptTreeNode(UserDept dept) {
//		deleteTreeNode(dept.getUdId());
//	}
//	
//	public void reloadDeptTreeNode(Dept dept) {
//		TreeNode node= getTreeNode(dept.getDId());
//		node.setBindData(dept);
//	}
//	public void reloadDeptTreeNode(UserDept dept) {
//		TreeNode node= getTreeNode(dept.getUdId());
//		node.setBindData(dept);
//	}
}

